package com.ykzb.project.dao.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ykzb.classify.mapper.ClassifyMapper;
import com.ykzb.project.dao.IProjectDao;
import com.ykzb.project.mapper.CategoryMapper;
import com.ykzb.project.mapper.ProjectMapper;
import com.ykzb.project.model.ProjectInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mac on 2017/3/31.
 */
@SuppressWarnings("restriction")
@Repository("projectDao")
public class ProjectDaoImpl implements IProjectDao{

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageInfo<ProjectInfo> selectProject(String minMoney, String maxMoney, String state, String category, String currPage, String pageSize) throws Exception {
        Long categoryId = null;
        if(StringUtils.isNotEmpty(category)){
            categoryId = categoryMapper.getIdByName(category);
        }
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("minMoney",minMoney);
        param.put("maxMoney",maxMoney);
        param.put("state",state);
        param.put("categoryId",categoryId);
        param.put("currPage",currPage);
        param.put("pageSize",pageSize);


        //分页处理，显示第一页的10条数据
        PageHelper.startPage(1, 10);
        List<ProjectInfo> list = projectMapper.selectProject(param);//查询
        if (!CollectionUtils.isEmpty(list)){
            for(ProjectInfo project:list){
                project.setCategoryName(categoryMapper.getNameById(project.getCategoryId()));
            }
        }
        // 取分页信息
        PageInfo<ProjectInfo> pageInfo = new PageInfo<ProjectInfo>(list);

        return pageInfo;
    }

    @Override
    public List<ProjectInfo> selectRecomendProject(Integer num) throws Exception {
        return projectMapper.selectRecomendProject(num);
    }

    @Override
    public ProjectInfo selectProjectById(Long id) throws Exception {
        if (id==null){
            return null;
        }else{
            return projectMapper.selectProjectById(id);
        }
    }


}
